<template>
  <div class="work-report-detail-page">
    <el-card class="content-card" shadow="never" v-loading="loading">
      <!-- 标题和头部信息 -->
      <div class="page-header">
        <div class="header-left">
          <h2 class="page-title">{{ detailInfo.teamName }}__{{ detailInfo.creator }}</h2>
          <div class="header-meta">
            <span>创建人: {{ detailInfo.creator }}</span>
            <span>创建时间: {{ detailInfo.createTime }}</span>
          </div>
        </div>
        <div class="header-right">
          <el-tag :type="statusTagType(detailInfo.status)" class="status-tag" effect="plain">
            {{ detailInfo.status }}
          </el-tag>
          <div class="header-actions">
            <el-button @click="handleDelete">删除</el-button>
            <el-button @click="handlePrint">打印</el-button>
          </div>
        </div>
      </div>

      <!-- 标签页 -->
      <el-tabs v-model="activeTab" class="detail-tabs">
        <el-tab-pane label="基础信息" name="basic">
          <!-- 报工信息 -->
          <div class="info-section">
            <div class="section-title">报工信息</div>
            <el-row :gutter="24" class="info-row">
              <el-col :span="8">
                <div class="info-item">
                  <span class="info-label">报工人员:</span>
                  <span class="info-value">{{ detailInfo.reportUser }}</span>
                </div>
              </el-col>
              <el-col :span="8">
                <div class="info-item">
                  <span class="info-label">报工时间:</span>
                  <span class="info-value">{{ detailInfo.reportTime }}</span>
                </div>
              </el-col>
              <el-col :span="8">
                <div class="info-item">
                  <span class="info-label">报工数量:</span>
                  <span class="info-value">{{ detailInfo.reportQty }}</span>
                </div>
              </el-col>
            </el-row>
          </div>

          <!-- 任务信息 -->
          <div class="info-section">
            <div class="section-title">任务信息</div>
            <el-row :gutter="24" class="info-row">
              <el-col :span="12">
                <div class="info-item">
                  <span class="info-label">任务名称:</span>
                  <span class="info-value">{{ detailInfo.taskName }}</span>
                </div>
                <div class="info-item">
                  <span class="info-label">站点名称:</span>
                  <span class="info-value">{{ detailInfo.siteName }}</span>
                </div>
                <div class="info-item">
                  <span class="info-label">工序名称:</span>
                  <span class="info-value">{{ detailInfo.processName }}</span>
                </div>
                <div class="info-item">
                  <span class="info-label">班组:</span>
                  <span class="info-value">{{ detailInfo.teamName2 || detailInfo.teamName }}</span>
                </div>
              </el-col>
              <el-col :span="12">
                <div class="info-item">
                  <span class="info-label">任务编号:</span>
                  <span class="info-value">{{ detailInfo.taskCode }}</span>
                </div>
                <div class="info-item">
                  <span class="info-label">工艺路线:</span>
                  <span class="info-value">{{ detailInfo.processRouteName }}</span>
                </div>
                <div class="info-item">
                  <span class="info-label">工序编号:</span>
                  <span class="info-value">{{ detailInfo.processCode }}</span>
                </div>
                <div class="info-item">
                  <span class="info-label">任务负责人:</span>
                  <span class="info-value">{{ detailInfo.taskOwner }}</span>
                </div>
              </el-col>
            </el-row>
          </div>

          <!-- 工单信息 -->
          <div class="info-section">
            <div class="section-title">工单信息</div>
            <el-row :gutter="24" class="info-row">
              <el-col :span="12">
                <div class="info-item">
                  <span class="info-label">工单名称:</span>
                  <span class="info-value">{{ detailInfo.orderName }}</span>
                </div>
                <div class="info-item">
                  <span class="info-label">产品名称:</span>
                  <span class="info-value">{{ detailInfo.productName }}</span>
                </div>
                <div class="info-item">
                  <span class="info-label">规格型号:</span>
                  <span class="info-value">{{ detailInfo.productModel }}</span>
                </div>
                <div class="info-item">
                  <span class="info-label">单位:</span>
                  <span class="info-value">{{ detailInfo.productUnit }}</span>
                </div>
                <div class="info-item">
                  <span class="info-label">批次:</span>
                  <span class="info-value">{{ detailInfo.batch }}</span>
                </div>
              </el-col>
              <el-col :span="12">
                <div class="info-item">
                  <span class="info-label">工单编号:</span>
                  <span class="info-value">{{ detailInfo.orderCode }}</span>
                </div>
                <div class="info-item">
                  <span class="info-label">产品编号:</span>
                  <span class="info-value">{{ detailInfo.productCode }}</span>
                </div>
                <div class="info-item">
                  <span class="info-label">产品类型:</span>
                  <span class="info-value">{{ detailInfo.productType }}</span>
                </div>
                <div class="info-item">
                  <span class="info-label">BOM:</span>
                  <span class="info-value">{{ detailInfo.bomCode }}</span>
                </div>
              </el-col>
            </el-row>
          </div>
        </el-tab-pane>

        <el-tab-pane label="质检内容" name="inspection">
          <div class="info-section">
            <div class="section-title">质检内容</div>
            <el-empty description="暂无质检内容" />
          </div>
        </el-tab-pane>
      </el-tabs>

      <!-- 底部按钮 -->
      <div class="footer-actions">
        <el-button @click="handleClose">关闭</el-button>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ElMessage, ElMessageBox } from 'element-plus'
import { computed, onMounted, reactive, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import https from '@/Http/http'
import { extractPayload, getErrorMessage, getResponseMessage, isRequestSuccess } from '@/utils/responseHelper'

interface WorkReportDetail {
  id: number
  inspectionName: string
  inspectionCode: string
  teamName: string
  creator: string
  createTime: string
  status: string
  reportUser: string
  reportTime: string
  reportQty: number
  taskName: string
  taskCode: string
  siteName: string
  processName: string
  processCode: string
  processRouteName: string
  teamName2: string
  taskOwner: string
  orderName: string
  orderCode: string
  productName: string
  productCode: string
  productModel: string
  productUnit: string
  productType: string
  bomCode: string
  batch: string
}

const route = useRoute()
const router = useRouter()
const loading = ref(false)
const activeTab = ref('basic')

const reportId = computed(() => Number(route.query.id) || 0)

const detailInfo = reactive<WorkReportDetail>({
  id: 0,
  inspectionName: '',
  inspectionCode: '',
  teamName: '生产班组五',
  creator: '李小海',
  createTime: '2025-11-30 21:34:23',
  status: '未质检',
  reportUser: '赵子龙',
  reportTime: '2023-06-09 15:13:09',
  reportQty: 50,
  taskName: '产品0255生产任务',
  taskCode: 'RWBH0235',
  siteName: '站点一',
  processName: '工序一',
  processCode: 'GXBH2315',
  processRouteName: '工艺流程一',
  teamName2: '班组一',
  taskOwner: '赵子龙',
  orderName: '新产品工单',
  orderCode: 'GHBH02354',
  productName: '新产品0524',
  productCode: 'CPBH245',
  productModel: 'RED',
  productUnit: '个',
  productType: '半成品',
  bomCode: 'BOM0215646',
  batch: '021'
})

const statusTagType = (status: string) => {
  switch (status) {
    case '未质检':
      return 'warning'
    case '质检中':
      return 'info'
    case '已质检':
      return 'success'
    case '已关闭':
      return 'danger'
    default:
      return 'info'
  }
}

const loadReportDetail = async () => {
  loading.value = true
  console.log('加载报工详情，reportId:', reportId.value, 'route.query:', route.query)
  try {
    let data: any = null
    
    if (reportId.value) {
      try {
        const response = await https.Reads.get('/AShow/WorkReportDetail', {
          params: { reportId: reportId.value }
        })
        const payload = extractPayload(response)
        if (isRequestSuccess(payload)) {
          data = payload?.data ?? payload?.Data
        }
      } catch (error) {
        console.warn('获取报工详情失败，使用示例数据:', error)
      }
    }

    // 如果没有API数据，使用示例数据（根据图片）
    if (!data) {
      // 数据已经在detailInfo中初始化，这里可以保持或更新
    } else {
      // 填充API返回的数据
      Object.assign(detailInfo, {
        id: data.id ?? reportId.value,
        inspectionName: data.inspectionName ?? '',
        inspectionCode: data.inspectionCode ?? '',
        teamName: data.teamName ?? '生产班组五',
        creator: data.creator ?? '李小海',
        createTime: data.createTime ?? '2025-11-30 21:34:23',
        status: data.status ?? '未质检',
        reportUser: data.reportUser ?? '赵子龙',
        reportTime: data.reportTime ?? '2023-06-09 15:13:09',
        reportQty: data.reportQty ?? 50,
        taskName: data.taskName ?? '产品0255生产任务',
        taskCode: data.taskCode ?? 'RWBH0235',
        siteName: data.siteName ?? '站点一',
        processName: data.processName ?? '工序一',
        processCode: data.processCode ?? 'GXBH2315',
        processRouteName: data.processRouteName ?? '工艺流程一',
        teamName2: data.teamName ?? '班组一',
        taskOwner: data.taskOwner ?? '赵子龙',
        orderName: data.orderName ?? '新产品工单',
        orderCode: data.orderCode ?? 'GHBH02354',
        productName: data.productName ?? '新产品0524',
        productCode: data.productCode ?? 'CPBH245',
        productModel: data.productModel ?? 'RED',
        productUnit: data.productUnit ?? '个',
        productType: data.productType ?? '半成品',
        bomCode: data.bomCode ?? 'BOM0215646',
        batch: data.batch ?? '021'
      })
    }
  } catch (error) {
    console.error('加载报工详情失败:', error)
  } finally {
    loading.value = false
  }
}

const handleDelete = async () => {
  try {
    await ElMessageBox.confirm('确定要删除这条报工记录吗？', '提示', {
      type: 'warning'
    })
    ElMessage.success('删除成功')
    router.back()
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('删除失败')
    }
  }
}

const handlePrint = () => {
  ElMessage.info('打印功能开发中')
  // 这里可以实现打印功能
  window.print()
}

const handleClose = () => {
  router.back()
}

onMounted(() => {
  loadReportDetail()
})
</script>


<style scoped>
.work-report-detail-page {
  padding: 16px;
  min-height: 100%;
  box-sizing: border-box;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}

.content-card {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.08);
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 24px;
  padding-bottom: 16px;
  border-bottom: 1px solid #ebeef5;
}

.header-left {
  flex: 1;
}

.page-title {
  margin: 0 0 12px 0;
  font-size: 20px;
  font-weight: 600;
  color: #303133;
}

.header-meta {
  display: flex;
  gap: 24px;
  font-size: 14px;
  color: #606266;
}

.header-right {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 12px;
}

.status-tag {
  border: 2px dashed #ff9800;
  border-radius: 20px;
  padding: 6px 20px;
  font-size: 14px;
  background: transparent;
  color: #ff9800;
}

.header-actions {
  display: flex;
  gap: 8px;
}

.detail-tabs {
  margin-top: 16px;
}

.info-section {
  margin-bottom: 32px;
}

.section-title {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 16px;
  padding-bottom: 8px;
  border-bottom: 2px solid #409EFF;
}

.info-row {
  margin-bottom: 8px;
}

.info-item {
  display: flex;
  margin-bottom: 12px;
  line-height: 1.8;
}

.info-label {
  min-width: 100px;
  color: #606266;
  font-weight: 500;
}

.info-value {
  flex: 1;
  color: #303133;
}

.footer-actions {
  display: flex;
  justify-content: center;
  gap: 12px;
  margin-top: 32px;
  padding-top: 16px;
  border-top: 1px solid #ebeef5;
}
</style>

